package at.fhj.itm.model;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;



public class VocabEntry 
{
	private String word; //the german word
	private String meaning; //the arabic meaning
	private String verbal; //the arabic word expressed in latin letters
	
	private static ByteArrayInputStream bais;
	private static DataInputStream dis;
	
	public static int STARTPOS_GERMAN=0;
	public static int STARTPOS_ARABIC=30;
	public static int ENDPOS_GERMAN=STARTPOS_ARABIC-1;
	public static int STARTPOS_VERBAL=69;
	public static int ENDPOS_ARABIC=STARTPOS_VERBAL-1;
	public static int ENDPOS_VERBAL=STARTPOS_VERBAL+49;
	
	//so that only a single method is needed to extract field data from a record
	public static String GERMAN_INDICATOR="GERMAN";
	public static String VERBAL_INDICATOR="VERBAL";
	public static String ARABIC_INDICATOR="ARABIC";
	
	public static String getWord(byte[] vocabRecord)
	{
		String german="";
		bais = new ByteArrayInputStream(vocabRecord);		
		dis = new DataInputStream(bais);
		
		try {
			german = dis.readUTF();//.substring(STARTPOS_GERMAN, ENDPOS_GERMAN-STARTPOS_GERMAN);
			System.out.println(german);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally
		{
			try {
				dis.close();
				bais.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
		}
		
//		german = new String(vocabRecord,STARTPOS_GERMAN,ENDPOS_GERMAN-STARTPOS_GERMAN).trim();
		System.out.println(german);
		return german;
	}

	public void setWord(String word) {
		this.word = word;
	}

	public static String getMeaning(byte[] vocabRecord) {
		String arabic="";
		bais = new ByteArrayInputStream(vocabRecord);
		dis = new DataInputStream(bais);
		try
		{
			arabic = dis.readUTF().substring(STARTPOS_ARABIC, ENDPOS_ARABIC-STARTPOS_ARABIC+1);
		}catch(Exception e){
			System.out.println(e.getMessage());
		}finally
		{
			try {
				dis.close();
				bais.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
		}
		return arabic;
	}

	public void setMeaning(String meaning) {
		this.meaning = meaning;
	}

	public static String getVerbal(byte[] vocabRecord) {
		String verbal="";
		bais = new ByteArrayInputStream(vocabRecord);
		dis = new DataInputStream(bais);
		
		try
		{
			verbal= dis.readUTF().substring(STARTPOS_VERBAL, ENDPOS_VERBAL-STARTPOS_VERBAL+1);
		}
		catch(Exception e)
		{
			System.out.println(e.getMessage());
		}finally
		{
			try {
				dis.close();
				bais.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
		}
//		verbal = new String(vocabRecord,STARTPOS_VERBAL,ENDPOS_VERBAL-STARTPOS_VERBAL).trim();
//		int verbalEndIndex = verbal.indexOf('\n');
//		if(verbalEndIndex<=ENDPOS_GERMAN)
//		{
//			return verbal.substring(0, verbalEndIndex);
//		}
//		else
//		{
//			return verbal;
//		}
		return verbal;
	}

	public void setVerbal(String verbal) {
		this.verbal = verbal;
	}

	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		VocabEntry other = (VocabEntry) obj;
		if (meaning == null) {
			if (other.meaning != null)
				return false;
		} else if (!meaning.equals(other.meaning))
			return false;
		if (word == null) {
			if (other.word != null)
				return false;
		} else if (!word.equals(other.word))
			return false;
		return true;
	}

	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((meaning == null) ? 0 : meaning.hashCode());
		result = prime * result + ((word == null) ? 0 : word.hashCode());
		return result;
	}

	public static String getAll(byte[] vocabRecord) 
	{
		return getWord(vocabRecord)+": "+getMeaning(vocabRecord)+
		" ("+getVerbal(vocabRecord)+")";
	}
	
	
}
